package demo.dcs.util;

import demo.java.lang.management.vo.DeviceInfo;
import demo.java.lang.management.vo.XmlySpan;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

public class RequestParameterParserUtil {
    private static Logger logger = LoggerFactory.getLogger(RequestParameterParserUtil.class);
    //设备cookie，cookie-value需要url encode
    private final static String DEVICE_COOKIE_KEY = "_device";

    //登陆信息cookie
    private final static String TOKEN_KEY = "_token";

    ///
    ///设备型号
    private final static String DEVICE_TYPE = "deviceType";

    private final static String SYMBOL_AND = "&";

    private final static String SYMBOL_DOT = ",";

    private final static String ENCODE_CHARACTER_SET = "UTF-8";

    //线上的environmentId
    private final static String ENVIRONMENTID_PRODUCT = "1";

    public static void fillXmlySpan(XmlySpan xmlySpan, Map<String, String> cookiesMap, String userAgentValue, String environmentId) {
        if (StringUtils.isBlank(environmentId)){
            environmentId = ENVIRONMENTID_PRODUCT;
        }
        parseFromRequest(xmlySpan, userAgentValue);
        convertMacAddress(xmlySpan);
    }

    public static void parseFromRequest(XmlySpan xmlySpan, String ua) {
        if (StringUtils.isNotBlank(ua) && (StringUtils.contains(ua, "ting")
                        || StringUtils.contains(ua, "ximalayababy"))) {
            //ios格式  ting_v1.0.6_c0 (CFNetwork, iPhone OS 6.1.3, iPhone4,1)
            if (StringUtils.containsIgnoreCase(ua, "CFNetwork")) {
                parseIOSUserAgent(xmlySpan, ua);
            } else if (StringUtils.containsIgnoreCase(ua, "Android")) {
                // android的格式 ting_3.25.1(NX505J,Android19)  永刚提供
                parseAndroidUserAgent(xmlySpan, ua);
            } else if (StringUtils.containsIgnoreCase(ua, "WP")){
                // WP的格式 ting_v1.1.33.0_c0 (WP 10.0.10586.494,Microsoft Corporation Surface Pro 4)
                parseWpUserAgent(xmlySpan, ua);
            }
        }
    }

    private static void parseAndroidUserAgent(XmlySpan xmlySpan, String ua) {
        int beginIndex = ua.indexOf("(");
        int endIndex = ua.indexOf(")");
        if (beginIndex > 0 && endIndex > beginIndex) {
            String content = ua.substring(beginIndex+1, endIndex);
            String[] arr = content.split(SYMBOL_DOT);
            if (arr.length == 2) {
                DeviceInfo deviceInfo = xmlySpan.getDeviceInfo();
                deviceInfo.setDeviceType(StringUtils.trim(arr[0]));
                deviceInfo.setOs(StringUtils.trim(arr[1]));
            }
        }
    }

    private static void convertMacAddress(XmlySpan xmlySpan) {
        DeviceInfo deviceInfo = xmlySpan.getDeviceInfo();
        String macAddress = deviceInfo.getMacAddress();
        String deviceName = deviceInfo.getDeviceName();
        if((StringUtils.isBlank(deviceName)|| StringUtils.equalsIgnoreCase(deviceName, "android")) && StringUtils.isNotBlank(macAddress) && macAddress.length() == 8){
            try {
                macAddress = MacAddressUtil.getFormateMacAddress(macAddress);
            } catch (IOException e) {
                logger.error("convertMacAddress error: " + e.getMessage());
            }
            deviceInfo.setMacAddress(macAddress);
        }
    }

    private static void parseWpUserAgent(XmlySpan xmlySpan, String ua) {
        int beginIndex = ua.indexOf("(");
        int endIndex = ua.indexOf(")");
        if (beginIndex > 0 && endIndex > beginIndex) {
            String content = ua.substring(beginIndex+1, endIndex);
            String[] arr = content.split(SYMBOL_DOT);
            if (arr.length > 1) {
                DeviceInfo deviceInfo = xmlySpan.getDeviceInfo();
                deviceInfo.setOs(StringUtils.trim(arr[0]));
                deviceInfo.setDeviceType(StringUtils.trim(arr[1]));
            }
        }
    }

    private static void parseIOSUserAgent(XmlySpan xmlySpan, String ua) {
        int beginIndex = ua.indexOf("(");
        int endIndex = ua.indexOf(")");
        if (beginIndex > 0 && endIndex > beginIndex) {
            String content = ua.substring(beginIndex+1, endIndex);
            String[] arr = content.split(SYMBOL_DOT);
            if (arr.length >= 3) {
                DeviceInfo deviceInfo = xmlySpan.getDeviceInfo();
                deviceInfo.setOs(StringUtils.trim(arr[1]));
                String deviceType = StringUtils.trim(arr[2]);
                if (arr.length >= 4) {
                    deviceType = deviceType + "-" + StringUtils.trim(arr[3]);//如iphone4-1表示iPhone4的1代
                }
                deviceInfo.setDeviceType(deviceType);
            }
        }
    }

}
